package com.example.xymarket.common;

import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

import com.example.xymarket.httpUtil.ImageService;
import com.example.xymarket.po.Goods;
import com.example.xymarket.po.Order;
import com.example.xymarket.po.Result;
import com.example.xymarket.po.UserInfo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MyJson {
    public static void JsonToToken(String Json) {
        try {
            /**得到返回的整个信息**/
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(Json).getAsJsonObject();
            JsonObject data = jsonObject.getAsJsonObject("data");
            UserInfo obj = gson.fromJson(data, UserInfo.class);
            Info.setMyUserInfo(new UserInfo());
            Info.getMyUserInfo().setToken(obj.getToken());
            Info.getMyUserInfo().setName(obj.getName());
            Info.getMyUserInfo().setId(obj.getId());
        } catch (Exception e) {

        }
    }

    public static void JsonToUserInfo(String Json) {
        try {
            /**得到返回的整个信息**/
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(Json).getAsJsonObject();
            /**取出Msg 为String形式**/

            /**取出Data 为Json**/
            JsonObject data = jsonObject.getAsJsonObject("data");
            UserInfo obj = gson.fromJson(data, UserInfo.class);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Info.setMyUserAvatar(new BitmapDrawable());
                    Info.setMyUserAvatar(ImageService.getBitmap(MyUrl.ImagesUrl+obj.getAvatar()));
                }
            }).start();
            Info.getMyUserInfo().setAvatar(obj.getAvatar());
        } catch (Exception e) {

        }
    }


    public static Integer getCode(String Json) {
        try {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(Json).getAsJsonObject();
            String code = jsonObject.get("code").toString();
            return Integer.parseInt(code);
        } catch (Exception e) {
            return Integer.MAX_VALUE;
        }
    }

    public static String getImageUri(String Json) {
        try {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(Json).getAsJsonObject();
            JsonObject data=jsonObject.getAsJsonObject("data");
            String uri = data.get("uri").getAsString();
            return uri;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * Map转Json对象
     * @return 转换的Json_String串
     */
    public static  <K,V>String MapToJsonString(Map<K,V> map)
    {
        String s="";
        try {
            Gson gson = new Gson();
            s=gson.toJson(map);
        }
        catch (Exception e)
        {
            Log.d("error",e.toString());
        }
        return s;
    }


    /**
     * 通过传递Result的Type来进行解析，返回Result对象。
     * @param Json 要解析的Json串
     * @param type Type type = new TypeToken<Result<需要的类型>>() {}.getType();
     * @return Result
     */
    public static Result getList(String Json,Type type) {
        try {
            /*获取data*/
            Gson gson = new Gson();
            Result result = gson.fromJson(Json, type);
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    public static Map<String,Object> getOrder(String Json)
    {
        Map<String, Object> map = new HashMap<>();
        try {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(Json).getAsJsonObject();
            String code = jsonObject.get("code").toString();
            map.put("code", Integer.parseInt(code));
            JsonArray data = jsonObject.getAsJsonArray("data");
            List<Order> orders = new ArrayList<>();
            for (JsonElement jsonObject1 : data) {
                Order order = gson.fromJson(jsonObject1, Order.class);
                order.setGoods(gson.fromJson(order.getGoodsList(), Goods.class));
                orders.add(order);
            }
            map.put("list", orders);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        finally {
            return map;
        }
    }

}